﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class UserControls_Location : System.Web.UI.UserControl
{
    private int _Province = 0;
    public int Province
    {
        get { return _Province; }
        set { _Province = value; }
    }

    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        CreateLocation();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        
    }

    private void CreateLocation()
    {
        _Province = SessionManager.CurrentSearch.Province;
        //Location Link
        LinkButton link = null;
        //Spacer
        Literal ltr = null;

        if (_Province == 0)
        {
            var provinceList = from p in HandBookData.EatDrinkDB.Provinces
                               orderby p.Order ascending
                               select p;
            foreach (HandBook.Data.Province p in provinceList)
            {
                link = new LinkButton();
                link.ID = p.ProvinceID.ToString();
                link.Text = p.ProvinceName;
                link.CommandName = "Province";
                link.CommandArgument = p.ProvinceID.ToString();

                link.Click += new EventHandler(Location_Click);

                uxLocation.Controls.Add(link);

                ltr = new Literal();
                ltr.Text = " | ";
                uxLocation.Controls.Add(ltr);
            }
        }
        else
        {
            var locationList = from loc in HandBookData.EatDrinkDB.Locations
                               where loc.ProvinceID.Equals(_Province)
                               select loc;
            //Nation
            link = new LinkButton();
            link.ID = "N0";
            link.Text = "Tất Cả";
            link.CommandName = "Nation";
            link.CommandArgument = "0";
            //Fire Event
            link.Click += new EventHandler(Location_Click);
            uxLocation.Controls.Add(link);

            ltr = new Literal();
            ltr.Text = "&#8594;";
            uxLocation.Controls.Add(ltr);


            bool hasProvince = false;
            foreach (HandBook.Data.Location loc in locationList)
            {
                //Province
                if (!hasProvince)
                {
                    link = new LinkButton();
                    link.ID = "P" + loc.ProvinceID.ToString();
                    link.Text = loc.Province.ProvinceName;
                    link.CommandName = "Province";
                    link.CommandArgument = loc.Province.ProvinceID.ToString();
                    //Fire Event
                    link.Click += new EventHandler(Location_Click);
                    uxLocation.Controls.Add(link);

                    ltr = new Literal();
                    ltr.Text = "&#8594;";
                    uxLocation.Controls.Add(ltr);
                    hasProvince = true;
                }

                //District
                link = new LinkButton();
                link.ID = "D" + loc.LocationID.ToString();
                link.Text = loc.DistrictShort;
                link.CommandName = "District";
                link.CommandArgument = loc.LocationID.ToString();
                //Fire Event
                link.Click += new EventHandler(Location_Click);
                uxLocation.Controls.Add(link);

                ltr = new Literal();
                ltr.Text = " | ";
                uxLocation.Controls.Add(ltr);
            }
        }
    }

    void Location_Click(object sender, EventArgs e)
    {
        BasePage page = (BasePage)Page;
        SessionManager.CurrentSearch.Location = Convert.ToInt32(((LinkButton)sender).CommandArgument);
        page.RebindData();
    }
}
